//
//  YYYHomeViewModel.swift
//  HotNews
//
//  Created by YYY on 2017/10/25.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import Foundation

class YYYHomeViewModel: CustomStringConvertible
{
    var homeModel: YYYHomeStatusModel
    
    //会员图标
    var memberImage: UIImage?
    //认证图标
    var vertifyImage: UIImage?
    
    //底部工具栏转发内容
    var retweetString: String?
    
    //评论
    var commentString: String?
    
    //赞
    var likeString: String?
    
    //微博来源
    var sourceString: String?
    
    //配图视图大小
    var pictureSize = CGSize()
    
    //姓名宽度
    var nameWidth: CGFloat = 0
    
    //正文高度
    var contextHeight: CGFloat = 0
    
    //正文的属性文本
    var contextAttributed: NSAttributedString?
    
    //被转发微博的属性文本
    var retweetTextAttributed: NSAttributedString?
    
    //被转发微博文本高度
    var retweetTextHeight: CGFloat = 0
    
    //行高
    var rowHeight: CGFloat = 0

    //配图URL的分辨
    var picUrls: [YYYPictureModel]?
    {
        return homeModel.retweeted_status?.pic_urls ?? homeModel.pic_urls
    }
    
    //数据加载展示的时候，仅执行第一次
    init(model: YYYHomeStatusModel)
    {
        homeModel = model
        //会员图标
        let rank = homeModel.user?.mbrank ?? 1
        if rank>=1 && rank<7
        {
            let imageName = "common_icon_membership_level\(rank)"
            memberImage = UIImage(named: imageName)
        }
        
        //认证类型
        //-1:没有认证
        //0:认证用户
        //2,3,5:企业认证
        //220:达人
        //认证图标
        let vertifyNumber = homeModel.user?.verified_type ?? -1
        switch vertifyNumber
        {
        case 0:
            //认证用户
            vertifyImage = UIImage(named: "avatar_vip")
            break
        case 2,3,5:
            //企业认证
            vertifyImage = UIImage(named: "avatar_enterprise_vip")
            break
        case 220:
            //达人
            vertifyImage = UIImage(named: "avatar_grassroot")
            break
        default:
            break
        
        }
        
        //微博来源
        sourceString = "来自" + (homeModel.source?.hrefLinkText()?.text ?? "")
        
        //转发、评论、赞
        retweetString = toolBarText(count: homeModel.reposts_count, defaultStr: "转发")
        
        commentString = toolBarText(count: homeModel.comments_count, defaultStr: "评论")
        
        likeString = toolBarText(count: homeModel.attitudes_count, defaultStr: "赞")
        
        //配图视图大小
        pictureSize = calculatePictureViewSize(count: picUrls?.count)
        
        //姓名宽度
        if let name = homeModel.user?.screen_name
        {
            nameWidth = name.calculateLabelSize(fontSize: 15, size: CGSize(width: CGFloat(MAXFLOAT), height: 20)).width
        }
        
        //文本大小
        let textSize = CGSize(width: kDeviceWidth - 24, height: CGFloat(MAXFLOAT))
        //字体大学
        let contextFont = UIFont.systemFont(ofSize: 15)
        let retweetFont = UIFont.systemFont(ofSize: 14)
        
        //正文属性文本高度
        if let text = homeModel.text
        {
            contextAttributed = EmoticonManager.shared.emoticonString(string: text, font: contextFont)
            contextHeight = contextAttributed?.boundingRect(with: textSize, options: [.usesLineFragmentOrigin], context: nil).height ?? 0
        }
        
        //被转发微博文本的高度
        if homeModel.retweeted_status != nil
        {
            let nameStr = homeModel.retweeted_status?.user?.screen_name ?? ""
            let textStr = homeModel.retweeted_status?.text ?? ""
            let retweetTextStr = "@" + nameStr + ":" + textStr
            retweetTextAttributed = EmoticonManager.shared.emoticonString(string: retweetTextStr, font: retweetFont)
            retweetTextHeight = retweetTextAttributed?.boundingRect(with: textSize, options: [.usesLineFragmentOrigin], context: nil).height ?? 0
        }
        
        //计算行高
        calculateRowHeight()
    }

    /// 设置底部分栏文字
    ///
    /// - Parameters:
    ///   - count: 数量
    ///   - defaultStr: 默认文字
    /// - Returns: 展示文字
    private func toolBarText(count: Int,defaultStr: String) -> String
    {
        if count == 0
        {
            return defaultStr
        }
        if count < 10000
        {
            return count.description
        }
        
        return String(format: "%.2f 万",CGFloat(count/10000))
    }
    
    
    /// 根据图片数量，计算配图视图的大小
    /// - Parameter count: 图片数量
    /// - Returns: 配图视图大小
    private func calculatePictureViewSize(count: Int?) -> CGSize
    {
        if count == 0 || count == nil
        {
            return CGSize()
        }

        //计算配图视图大小
        let row = (count! - 1)/3 + 1
        var height = kPictureViewOutterMargin
        height += CGFloat(row - 1) * kPictureViewInnerMargin
        height += CGFloat(row) * kPictureItemWidth
        
        return CGSize(width: kPictureViewWidth, height: height)
    }

    /// 更新单张图像的大小
    ///
    /// - Parameter image: 显示的单张图片
    func updateSingleImageSize(image: UIImage)
    {
        rowHeight -= pictureSize.height

        var size = image.size
        let maxWidth: CGFloat = 200
        let minWidth: CGFloat = 40

        //图片过宽处理
        if size.width > maxWidth
        {
            size.width = maxWidth
            size.height = size.width * image.size.height/image.size.width
        }
        //图片过窄处理
        if size.width < minWidth
        {
            size.width = minWidth
            size.height = size.width * image.size.height/image.size.width
        }
        //图片过高处理
        if size.height > 200
        {
            size.height = 200
            size.width = size.height * image.size.width/image.size.height
        }
        size.height += kPictureViewOutterMargin
        pictureSize = size
        
        //计算行高
        rowHeight += pictureSize.height
    }
    
    //计算行高
    private func calculateRowHeight()
    {
        //准备常量
        //原创微博行高
        //顶部视图+间距+头像视图+间距+正文+ 配图视图+间距+底部工具栏
       
        //被转发微博行高
        //顶部视图+间距+头像视图+间距+正文+ 间距+间距+被转发文本+ 配图视图+间距+底部工具栏
        
        //间距
        let margin: CGFloat = 12
        //头像高度
        let avatarHeight: CGFloat = 34
        //正文高度
        //被转发微博的正文高度
        //配图高度
        //底部工具栏高度
        let toolBarHeight: CGFloat = 35
        
        var height: CGFloat = 0
        height += 2 * margin + avatarHeight + margin + contextHeight
        
        if homeModel.retweeted_status != nil
        {
            height += 2 * margin + retweetTextHeight
        }
        height += pictureSize.height + margin + toolBarHeight
        
        //设置行高
        rowHeight = height
    }
    
    var description: String
    {
        return homeModel.description
    }
}
